package com.sinodata.bsm.cicp.collector.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.constants.SeparatorConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;
import com.sinodata.bsm.common.vo.ResPropertyValue.PropValue;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-10-11 下午1:51:50          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class OneMergerNCollector extends Collector {

    private static final Logger LOGGER = Logger.getLogger(OneMergerNCollector.class);

    @Override
    public String[] getPropMeta() {
        return new String[] { "$1", "$2" };
    }

    @Override
    public String[] getParamMeta() {
        //用于产生事件的列的列号，从1开始
        return new String[] { "eventRowNumber", "shell", "separator" };
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> list = new ArrayList<ResPropertyValue>();
        String[] props = getPropMeta();
        String eventRowNumberStr = this.getParamMap().get("eventRowNumber");
        String shell = this.getParamMap().get("shell");
        String separator = this.getParamMap().get("separator");

        if (eventRowNumberStr == null || !eventRowNumberStr.matches("\\d+")) {
            throw new CollectorException("you set the wrong value for OneMergerNCollector's eventRowNumber ");
        }
        if (separator == null || separator.length() == 0) {
            separator = "\\s+";
        }

        int eventRowNumberInt = Integer.valueOf(eventRowNumberStr) - 1;

        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, shell, 0, 0);
            if (reader == null) {
                return null;
            }
            String currentLine = null;
            StringBuffer buffer = new StringBuffer();
            while ((currentLine = reader.readLine()) != null) {
                ResPropertyValue resPropertyValueX = new ResPropertyValue();
                list.add(resPropertyValueX);
                PropValue[] values = new ResPropertyValue.PropValue[props.length];
                resPropertyValueX.setValues(values);
                String[] spilts = currentLine.split(separator);
                values[0] = ResPropertyValue.createPropValue(spilts[eventRowNumberInt], props[0]);
                for (int i = 0; i < spilts.length; i++) {
                    buffer.append(spilts[i]);
                    buffer.append(SeparatorConstants.COL_SEPARATOR);
                }
                buffer.setLength(buffer.length() - SeparatorConstants.COL_SEPARATOR.length());
                buffer.append(SeparatorConstants.LIN_SEPARATOR);
            }
            buffer.setLength(buffer.length() - SeparatorConstants.LIN_SEPARATOR.length());
            PropValue[] lastValues = list.get(list.size() - 1).getValues();
            lastValues[lastValues.length - 1] = ResPropertyValue.createPropValue(buffer.toString(), props[props.length - 1]);
        } catch (IOException e) {
            LOGGER.error("OneMergerNCollector product error,ip[" + ip + "],shell[" + shell + "]");
            throw new CollectorException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    LOGGER.error("OneMergerNCollector 关闭流失败");
                }
            }
        }
        return list;
    }

}
